// Sphere object

#include <math.h>
#include <iostream>

#include "../include/planecicle.h"

#include "../my/include/Matrix.h"

// Plane defined as a vertex (in world space) as location
//                  and n which is the normal direction

PlaneCicle::PlaneCicle(Vertex &aa, Vector &nn,double rr) {
	a = aa;
	n = nn;
	typex = "plane";
	typei = 111100;
	r = rr;
}
bool PlaneCicle::intersect(Ray &input_ray, Hit *hit) {
	Ray ray=input_ray;
	//transform
	if (transformed != 0) {
		ray.D.multiply(invers_matrix);
		ray.P.multiply(invers_matrix);
	}

	double t = (a - ray.P).dot(n) / (ray.D.dot(n));


	if (t > 0.00001) {
		hit->obj = this;
		hit->t = t;

		hit->obj_mat = *this->obj_mat;

		hit->p.x = ray.P.x + t * ray.D.x;
		hit->p.y = ray.P.y + t * ray.D.y;
		hit->p.z = ray.P.z + t * ray.D.z;
		hit->p.w = 1.0;
		hit->n.x = n.x;
		hit->n.y = n.y;
		hit->n.z = n.z;

		double dis = hit->p.distance(a);
		if(dis > r){
			return false;
		}


		if (transformed != 0) {
			hit->n.multiply(invers_matrix);
		}
		hit->n.normalise();

		return true;
	} else {
		return false;
	}
}
